package com.idp.web.service.impl;

import com.idp.common.base.BusinessException;
import com.idp.common.constants.CommonConstants;
import com.idp.common.persistence.Page;
import com.idp.common.util.ResourceUtils;
import com.idp.web.dao.HospitalDepartmentDao;
import com.idp.web.entity.HospitalDepartment;
import com.idp.web.service.IHospitalDepartmentService;
import com.idp.web.system.entity.SysRoleMenu;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 医院与科室关系service实现类
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/2/28
 */
@Service("hospitalDepartmentService")
public class HospitalDepartmentServiceImpl implements IHospitalDepartmentService {

    @Resource
    private HospitalDepartmentDao hospitalDepartmentDao;

    /**
     * 根据条件分页查询医院科室关系列表
     *
     * @param hospitalDepartment 查询条件
     * @param page               分页信息
     * @return Page<HospitalDepartment>
     */
    @Override
    public Page<HospitalDepartment> findByPage(HospitalDepartment hospitalDepartment, Page<HospitalDepartment> page) {
        page.setResult(hospitalDepartmentDao.find(hospitalDepartment, page));
        return page;
    }

    /**
     * 根据条件查询医院科室关系列表
     *
     * @param hospitalDepartment 查询条件
     * @return List<HospitalDepartment>
     */
    @Override
    public List<HospitalDepartment> findBySearch(HospitalDepartment hospitalDepartment) {
        return hospitalDepartmentDao.find(hospitalDepartment);
    }

    /**
     * 根据id查询对应的医院科室关系信息
     *
     * @param id 主键id
     * @return HospitalDepartment
     */
    @Override
    public HospitalDepartment getById(String id) {
        return hospitalDepartmentDao.getById(id);
    }

    /**
     * 新增
     *
     * @param hospitalDepartment 医院科室关系信息
     * @param userId 用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void add(HospitalDepartment hospitalDepartment,String userId) {
        //新建医院和科室关系表
        hospitalDepartment.preInsert(userId);
        hospitalDepartmentDao.add(hospitalDepartment);
    }

    /**
     * 修改
     *
     * @param hospitalDepartment 医院科室关系信息
     * @param userId 用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void update(HospitalDepartment hospitalDepartment,String userId) {
        hospitalDepartmentDao.update(hospitalDepartment);
    }

    /**
     * 添加或者修改
     *
     * @param hospitalId    医院id
     * @param departmentIds 科室ids
     * @param userId        用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public boolean saveOrUpdate(String hospitalId, String departmentIds, String userId){
        if (
                StringUtils.isEmpty(hospitalId)
                        || StringUtils.isEmpty(departmentIds)
                        || CommonConstants.STRING_COMMA.equals(departmentIds.trim())
                        || StringUtils.isEmpty(userId)
        ){
            System.out.println("参数错误");
            return false;
        }
        departmentIds = departmentIds.trim();
        hospitalId = hospitalId.trim();

        HospitalDepartment hospitalDepartment;

        //删除医院下科室
        hospitalDepartmentDao.deleteByHospital(hospitalId);

        for(String menuId : departmentIds.split(CommonConstants.STRING_COMMA)){
            if(StringUtils.isNotEmpty(menuId)){
                menuId = menuId.trim();
                hospitalDepartment = new HospitalDepartment();
                hospitalDepartment.preInsert(userId);
                hospitalDepartment.setDepartmentId(menuId.trim());
                hospitalDepartment.setSort(0);
                hospitalDepartment.setHospitalId(hospitalId);
                hospitalDepartmentDao.add(hospitalDepartment);
            }
        }

        return true;
    }

    /**
     * 根据医院id和科室id删除对应的医院科室信息表信息
     *
     * @param hospitalId 医院id
     * @param departmentId 科室id
     * @return void
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void deleteBy(String hospitalId, String departmentId) {
        hospitalDepartmentDao.deleteBy(hospitalId,departmentId);
    }

    /**
     * 根据医院主键获取对应的医院下科室ids
     *
     * @param hospitalId 医院id
     */
    @Override
    public List<String> selectDepartmentIds(String hospitalId){
        return hospitalDepartmentDao.selectDepartmentIds(hospitalId);
    }

    /**
     * 根据医院id和科室id获取对应的医院科室信息表信息
     *
     * @param hospitalId 医院id
     * @param departmentId 科室id
     * @return HospitalDepartment
     */
    @Override
    public HospitalDepartment getHospitalDepartment(String hospitalId, String departmentId){
        if (StringUtils.isEmpty(hospitalId) || StringUtils.isEmpty(departmentId)){
            return null;
        }
        return  hospitalDepartmentDao.getHospitalDepartment(hospitalId.trim(),departmentId.trim());
    }

}